package com.concerneyes.mine.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.R;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class MyAccountAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private int[] listImg;
    private boolean isGoods;
    private GoodsListener listener;

    public interface GoodsListener {
        public void click(View v);
    }

    public MyAccountAdapter(Context context, boolean isGoods, GoodsListener listener) {
        this.context = context;
        this.isGoods = isGoods;
        this.listener = listener;
        listImg = new int[]{R.drawable.ic_text, R.drawable.ic_text,R.drawable.ic_text, R.drawable.ic_text,R.drawable.ic_text, R.drawable.ic_text};
    }

    @Override
    public int getCount() {
        return listImg.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_account, null);
            holder.ivAccount = (ImageView) convertView.findViewById(R.id.iv_account);
            holder.goodsName = (TextView) convertView.findViewById(R.id.goods_name);
            holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.change = (Button) convertView.findViewById(R.id.change);
            holder.returnMoney = (Button) convertView.findViewById(R.id.return_money);
            holder.llGoods = (LinearLayout) convertView.findViewById(R.id.ll_goods);
            holder.llReturn = (LinearLayout) convertView.findViewById(R.id.ll_return);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (isGoods) {
            holder.llGoods.setVisibility(View.VISIBLE);
            holder.llReturn.setVisibility(View.GONE);

            holder.change.setOnClickListener(this);
            holder.change.setTag(position);
            holder.returnMoney.setOnClickListener(this);
            holder.returnMoney.setTag(position);
        } else {
            holder.llGoods.setVisibility(View.GONE);
            holder.llReturn.setVisibility(View.VISIBLE);
        }
        holder.ivAccount.setImageResource(listImg[position]);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change:
                listener.click(v);
                break;
            case R.id.return_money:
                listener.click(v);
                break;
        }

    }

    class ViewHolder {
        ImageView ivAccount;
        TextView goodsName;
        TextView price;
        Button change;
        Button returnMoney;
        LinearLayout llGoods;
        LinearLayout llReturn;
    }
}
